package com.renwei.yunlong.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.OwnerSpareInquireAdapter;
import com.renwei.yunlong.adapter.ServiceSpareInquireAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerNewInquireBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.SpareInquireBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.renwei.yunlong.utils.ToastUtil;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 备件库查询--页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class SpareInquireActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_scroll)
    ImageView ivScroll;
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;//搜索文字
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.et_search)
    EditText etSearch;

    private ACache mCache;
    private ServiceSpareInquireAdapter serviceAdapter;
    private OwnerSpareInquireAdapter ownerAdapter;

    String companyType;
    String search;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_spare_inquire);
        ButterKnife.bind(this);
        mCache = ACache.get(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);
        initData();

    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivScroll.setVisibility(View.VISIBLE);
        ivScroll.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        tvTitle.setText("备件库查询");

        rlSearch.setOnClickListener(this);

        companyType = mCache.getAsString("companyType");
        search = etSearch.getText().toString().trim();

        if ("1".equals(companyType)) {
            //{"currentUserId":当前登录人ID、"ownerCode":当前公司编码,"hardName":此页面查询条件,"typeStatus":状态,page:第几页，rows:每页显示条数}
            LoginBean loginBean = (LoginBean) mCache.getAsObject("loginBean");
            if (loginBean != null){
                String jsonBean = "{currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\",ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",hardName:\"" +""+ "\",typeSturts:\"" + "0" + "\",page:\"" + "1" + "\",rows:\"" + "100000" + "\"}";
                ownerNewInquireHttp(jsonBean);
            }
        } else if ("2".equals(companyType)) {
            //{"currentUserId":当前登录人ID、"ownerCode":当前公司编码,"hardName":此页面查询条件,"typeStatus":状态,page:第几页，rows:每页显示条数}
            ServiceLoginBean serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
            if (serviceLoginBean != null){
                String jsonBean = "{currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",ownerCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardName:\"" + "" + "\",typeSturts:\"" + "1" + "\",page:\"" + "1" + "\",rows:\"" + "100000" + "\"}";
                serviceNewInquireHttp(jsonBean);
            }
        }
    }

    //业主 备件查询
    private void ownerNewInquireHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SpareInquireActivity.this);
        hp.setUrl(Api.OWNER_QUERYHARDWARE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商 备件查询
    private void serviceNewInquireHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SpareInquireActivity.this);
        hp.setUrl(Api.SERVICE_QUERYHARDWARE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_scroll:
                break;
            case R.id.rl_search:

                /**
                 * 暂未实现分页
                 *
                 *
                 *
                 */
                if ("1".equals(companyType)) {
                    //{"currentUserId":当前登录人ID、"ownerCode":当前公司编码,"hardName":此页面查询条件,"typeStatus":状态,page:第几页，rows:每页显示条数}
                    LoginBean loginBean = (LoginBean) mCache.getAsObject("loginBean");
                    if (loginBean != null){
                        if (!TextUtils.isEmpty(search)){
                            String jsonBean = "{currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\",ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",hardName:\"" +search + "\",typeSturts:\"" + "0" + "\",page:\"" + "1" + "\",rows:\"" + "5" + "\"}";
                            ownerNewInquireHttp(jsonBean);
                        }
                    }

                } else if ("2".equals(companyType)) {
                    //{"currentUserId":当前登录人ID、"ownerCode":当前公司编码,"hardName":此页面查询条件,"typeStatus":状态,page:第几页，rows:每页显示条数}
                    ServiceLoginBean serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
                    if (serviceLoginBean != null){
                        if (!TextUtils.isEmpty(search)){
                            String jsonBean = "{currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",ownerCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardName:\"" + search + "\",typeSturts:\"" + "1" + "\",page:\"" + "1" + "\",rows:\"" + "5" + "\"}";
                            serviceNewInquireHttp(jsonBean);
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    private void setOwnerListDate(List<SpareInquireBean.Rows> mList) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ownerAdapter = new OwnerSpareInquireAdapter(SpareInquireActivity.this, mList);
        ownerAdapter.setOnItemClickListener(new OwnerSpareInquireAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name) {

            }
        });
        recyclerView.setAdapter(ownerAdapter);
    }

    private void setServiceListDate(List<SpareInquireBean.Rows> mList) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        serviceAdapter = new ServiceSpareInquireAdapter(SpareInquireActivity.this, mList);
        serviceAdapter.setOnItemClickListener(new ServiceSpareInquireAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name) {

            }
        });
        recyclerView.setAdapter(serviceAdapter);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                SpareInquireBean ownerBean = new Gson().fromJson(result,SpareInquireBean.class);
                if (ownerBean.getMessage().getCode() == 200){
                    setOwnerListDate(ownerBean.getRows());
                }else if (ownerBean.getMessage().getCode() == 1008){//无权限访问
                    ToastUtil.showShort(this,"无权限访问");
                }
                break;
            case 2:
                SpareInquireBean serviceBean = new Gson().fromJson(result,SpareInquireBean.class);
                if (serviceBean.getMessage().getCode() == 200){
                    setServiceListDate(serviceBean.getRows());
                }else if (serviceBean.getMessage().getCode() == 1008){//无权限访问
                    ToastUtil.showShort(this,"无权限访问");
                }
                break;
            default:
                break;
        }
    }
    @Override
    public void onException(int requestId, String code) {

    }
}
